package fc.testmain;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import fc.entity.OaEntity;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.upgradetool.wscheck.Util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class AAAABBB {
    public static void main(String[] args) {
        String API_NAME="ZH_YFSY_XQ";

        BaseBean log = new BaseBean();


        OaEntity oe=new OaEntity();

        String requestid = "957361";
        String tablename = "formtable_main_558";
        oe.setREQUESTID(requestid);

        String sql=
                "SELECT requestid,工程案号 GCAH,实验单号 SYDH,项目编号 XMBH,实验名称 SYMC from ( select requestid,syah 工程案号 ,sydh  实验单号,xmbh 项目编号,symc 实验名称  from "+tablename+"  o1  with(nolock)  where requestid="+requestid+")t ";
        RecordSet rs = new RecordSet();
        rs.executeSql(sql);
        if(rs.next()){
            oe.setGCAH(Util.null2String(rs.getString("GCAH")));
            oe.setSYDH(Util.null2String(rs.getString("SYDH")));
            oe.setXMBH(Util.null2String(rs.getString("XMBH")));
            oe.setSYMC(Util.null2String(rs.getString("SYMC")));


        }


        String sql1="select 料件编号 LJBH,数量 SL,单位 DW,物料需求时间 WLXQSJ from (select  o1.id,o1.ljbht100 料件编号,o1.gghsl 数量,o1.dw 单位,o2.wlxqsj 物料需求时间 " +
                "from "+tablename+"_dt1 o1 with(nolock) ,"+tablename+" o2 with(nolock)  " +
                "where o1.mainid=o2.id  and o1.gglb='0' and o2.requestid="+requestid+")tt   order by id  ";

        RecordSet rs1 = new RecordSet();
        rs1.executeSql(sql1);
        while(rs1.next()){
            List<OaEntity> dts=oe.getDT1();
            OaEntity dt1=new OaEntity();

            dt1.setLJBH(Util.null2String(rs1.getString("LJBH")));
            dt1.setSL(Util.null2String(rs1.getString("SL")));
            dt1.setDW(Util.null2String(rs1.getString("DW")));
            dt1.setWLXQSJ(Util.null2String(rs1.getString("WLXQSJ")));
            dt1.setWLXQSJ(Util.null2String(rs1.getString("WLXQSJ")));


            dts.add(dt1);
            oe.setDT1(dts);
        }


        String testparam="";

        testparam="";
        for(int i=0;i<oe.getDT1().size();i++){
            log.writeLog("料件编号 LJBH:"+oe.getDT1().get(i).getLJBH()+
                    ",数量 SL:"+oe.getDT1().get(i).getSL()+
                    ",单位 DW:"+oe.getDT1().get(i).getDW()+
                    ",物料需求时间 WLXQSJ:"+oe.getDT1().get(i).getWLXQSJ()
            );
            //testparam+="料件编号 LJBH:"+oe.getDT1().get(i).getLJBH()+ ",数量 SL:"+oe.getDT1().get(i).getSL()+ ",单位 DW:"+oe.getDT1().get(i).getDW()+ ",物料需求时间 WLXQSJ:"+oe.getDT1().get(i).getWLXQSJ();
            if (!"".equals(testparam)){
                testparam=testparam+",";
            }
            testparam+="{" +
                    "\"idx\":"+"\""+(i+1)+"\","+
                    "\"code\":"+"\""+oe.getDT1().get(i).getLJBH()+"\","+
                    "\"requst_date\":"+"\""+oe.getDT1().get(i).getWLXQSJ()+"\","+
                    "\"amount\":"+"\""+oe.getDT1().get(i).getSL()+"\","+
                    "\"project\":"+"\""+oe.getXMBH()+"\","+
                    "\"memo\":"+"\""+oe.getSYMC()+"\""+
                    "}";

        }

        String user_code="YJGYZH";
        String head_memo=oe.getSYDH();//表头备注

        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String  timestamp= format.format(new Date());

        String json_param=
                "{"+
                        "\"key\": \"f5458f5c0f9022db743a7c0710145903\","+
                        "\"type\": \"sync\","+
                        "\"host\": {"+
                        "\"prod\": \"oa\","+
                        "\"ip\": \"192.168.0.1\","+
                        "\"lang\": \"zh_CN\","+
                        "\"acct\": \"tiptop\","+
                        "\"timestamp\": \""+timestamp+"\""+
                        "},"+
                        "\"service\": {"+
                        "\"prod\": \"T100\","+
                        "\"name\": \"apst300_crt\","+
                        "\"ip\": \"192.168.0.2\","+
                        "\"id\": \"topprd\""+
                        "},"+
                        "\"datakey\": {"+
                        "\"EntId\": \"2\","+
                        "\"CompanyId\": \"ZH00\""+
                        "},"+
                        "\"payload\": {"+
                        "\"std_data\": {"+
                        "\"parameter\": {"+
                        "\"p1\": \"\","+
                        "\"p2\": \"\""+
                        "}"+
                        "},"+
                        "\"param\": {"+
                        "\"top\": {"+
                        "\"user_code\": \""+user_code+"\","+
                        "\"memo\": \""+head_memo+"\""+
                        "},"+
                        "\"data\": ["+
                        testparam+
                        "]"+
                        "}"+
                        "}"+
                        "}";


        String result="ERROR";
        if(!"".equals(testparam)){
            String data = HttpRequest.post("http://192.168.0.108/wstopprd/ws/r/awsp920").body(json_param).execute().body();
            try {
                Object succesResponse = JSON.parse(data);
                Map map = (Map)succesResponse;
                Map map1 = (Map)map.get("payload");
                Map map2 = (Map)map1.get("std_data");
                Map map3 = (Map)map2.get("execution");
                if("0".equals(map3.get("code"))){
                    result="SUCCESS";
                }
            } catch (Exception e) {}
        }else{
            result="NOTNEED";
        }
    }
}
